//
//  ST_UpdateUserInfoCell.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/3.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import HandyJSON
import SwiftyAttributes

class ST_UpdateUserInfoCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = .none
        
        contentView.addSubview(info_label)
        contentView.addSubview(icon_image_view)
        contentView.addSubview(content_label)
        contentView.addSubview(nav_image_view)
        
        
        icon_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(22)
        }
        
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(50)
            make.centerY.equalToSuperview()
        }
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(info_label.snp.right).offset(10)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-35)
        }
        nav_image_view.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        
        let line = UIView()
        line.backgroundColor = Color.init(hex: 0xF2F4F7)
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(info_label).offset(-5)
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .right
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var nav_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("mycenter_icon_下一页")
        return control
    }()

    lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
//        control.contentMode = .scaleAspectFill
//        control.clipsToBounds = true
//        control.backgroundColor = Color.random
        return control
    }()
    
    
//    MARK: - Bind Model
    var model = ST_ModifyUserCellModel(){
        didSet{
            
            info_label.text = model.summaryTitle
            
            nav_image_view.isHidden = "星座" == model.summaryTitle
            
            if "身高" == model.summaryTitle{
                if model.title != "0"{
                    content_label.text = model.title + "cm"
                }else{
                    content_label.text = ""
                }
            }else{
                content_label.text = model.title
            }
            
            
            if let url =  model.icon.compress_image().url{
                
                icon_image_view.pin_setImage(from: url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
                
                icon_image_view.snp.updateConstraints { (make) in
                    make.width.height.equalTo(15)
                    make.left.equalToSuperview().offset(25+3)
                }
                
            }else{
                
                icon_image_view.image = UIImage.init(named: model.icon)
                
                icon_image_view.snp.updateConstraints { (make) in
                    make.left.equalToSuperview().offset(25)
                    make.width.height.equalTo(22)
                }
                
            }
            
        }
    }
}





class GeneralInfoCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = .none
        
        contentView.addSubview(info_label)
        contentView.addSubview(content_label)
        contentView.addSubview(nav_image_view)
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(15)
        }
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.right.equalToSuperview().offset(-40)
            make.top.equalToSuperview().offset(45)
            make.bottom.equalToSuperview().offset(-9)
        }
        nav_image_view.snp.makeConstraints { (make) in
            make.centerY.equalTo(info_label)
            make.right.equalToSuperview().offset(-15)
        }
        
        let line = UIView()
        line.backgroundColor = Color.init(hex: 0xF2F4F7)
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(info_label).offset(-5)
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .right
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var nav_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("mycenter_icon_下一页")
        return control
    }()

    
    
    
//    MARK: - Bind Model
    var model = ST_ModifyUserCellModel(){
        didSet{
            
            info_label.text = model.summaryTitle
            
            content_label.attributedText = model.titleAttribute
        }
    }
}

class ST_GeneralOfSwitchCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(info_label)
        
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
        }
        contentView.addSubview(select_control)
        select_control.snp.makeConstraints { (make) in
            make.centerY.equalTo(info_label)
            make.right.equalToSuperview().offset(-20)
        }
        
        let line = UIView()
        line.backgroundColor = Color.init(hex: 0xF2F4F7)
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(info_label).offset(-5)
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var select_control:UISwitch = {
      let control = UISwitch()
        control.onTintColor = UIColor.init(hex: 0xFEC207)
        control.backgroundColor = UIColor.init(hex: 0xACB4C0)
        control.cornerRadius = 16
        control.addTarget(self, action: #selector(_switch_handle), for: UIControl.Event.touchUpInside)
       return control
   }()
    
    var cellModel = ST_GeneralOfSwitchCellModel(){
        didSet{
            
            info_label.text = cellModel.infoKey
            
            select_control.isOn = IS_INVISIBLE
        }
    }
    
    @objc func _switch_handle(){
        

        if IS_INVISIBLE{
            
            self.setOnLineModel(modelInt: 1)
            
        }else{
            
            select_control.isOn = IS_INVISIBLE
            
            let attrString = NSMutableAttributedString(string: "隐身模式")
            
               let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
               attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
               
               let attrString1 = NSMutableAttributedString(string: "开启隐身模式后你将不会在伴心被别\n人看到，活跃状态也会隐藏！你仍然\n可以正常使用伴心，确认要开启吗？")
               
               let paragh = NSMutableParagraphStyle()
               paragh.alignment = .center
                paragh.lineSpacing = 3
            
               let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1),.paragraphStyle:paragh]
               
               attrString1.addAttributes(attr1, range: NSRange(location: 0, length: attrString1.length))
               
               
               let alert = BTVerticalAlertViewController.init(title: attrString, message: attrString1, imageName: "隐身模式_icon", confirmText: "确认开启", cancelText: "取消")
            
                alert.overCurrentContext = true
               
               alert.over_current_context_alpha = "0.4"

               alert.root_view_height = 357
               
               alert.root_view_width = 300
            
                alert.cover_image_size = CGSize.init(width: 90, height: 90)
            
                alert.confirm_closure = { [weak self] in
                 
                    
                    if FETCH_MEMBER_STATUS() != .subscribe{
                     
                        ST_MaidWayManager.shared.source = .开启隐身模式
                        
                        
                        let vc  = ST_MemberSubscriberController.init(subscribeType: ST_BitianSubscribeType.开启隐身模式)

                        vc.overCurrentContext = true

                        GlobalPresent(toController: vc)
                        
                    }else{
                        self?.setOnLineModel(modelInt: 2)
                    }
                }
               
               GlobalPresent(toController: alert)
            
        }
        
    }
    
    
    fileprivate func setOnLineModel(modelInt:Int){
    
        JVrequest(PH.setOnlineModeApi,parameters: ["online_mode":modelInt], success: { (json) in
            
            print("改变在线状态 成功:",modelInt)
            
            IS_INVISIBLE = modelInt == 2
            
            self.select_control.isOn = IS_INVISIBLE
            
        }) { (code, err) in
            
            self.select_control.isOn = IS_INVISIBLE
            
            self.parentViewController?.toast(text: err)
        }
        
//
    
    }
}


class ST_GeneralOfSwitchCellModel: ST_OtherGeneralCellModel {
    
}

class ST_OtherGeneralCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(info_label)
        contentView.addSubview(content_label)
        contentView.addSubview(nav_image_view)
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
        }
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(info_label.snp.right).offset(10)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-35)
        }
        nav_image_view.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        
        let line = UIView()
        line.backgroundColor = Color.init(hex: 0xF2F4F7)
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(info_label).offset(-5)
            make.height.equalTo(1)
            make.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .right
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var nav_image_view:UIImageView = {
        let control = UIImageView()
        control.image = ST_ImageUtils.imageName("mycenter_icon_下一页")
        return control
    }()

    var cellModel = ST_OtherGeneralCellModel(){
        didSet{
            
            info_label.text = cellModel.infoKey
            
            content_label.text = cellModel.infoValue
        }
    }
}

    

class ST_OtherGeneralCellModel: NSObject {
    
    var objKey = ""
    
    var infoKey = ""
    var infoValue = ""
}

class ST_ModifyUserCellModel: NSObject,HandyJSON {
    
    var id = ""
//    显示的值
    var title = ""
//    类型 input select other info
    var data_type = ""
    
    var icon = ""
//    需要补全
//    中文描述
    var summaryTitle = ""
//    修改的数据库key
    var modifyKey = ""
    
    
//    扩展
    var titleAttribute = NSAttributedString()
    
    var titleHeight = 0.cgFloat
    
//    -------
    var introCellHeight = 0.cgFloat
    
    var introCellColor = UIColor.white
    
    required override init() {
        super.init()
    }
    
    func create_attribute(){
        
        let p = NSMutableParagraphStyle()
        p.lineSpacing = 5
        
        titleAttribute = self.title.withAttributes([Attribute.textColor(UIColor.init(hexString: "#666660")!),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.paragraphStyle(p)])
        
        titleHeight = titleAttribute.height(withConstrainedWidth: ScreenWidth - 65)
    }
    
    
    //关于我
    func create_intro_attribute(){
        
        if self.summaryTitle == "我想认识的人是"{
            introCellColor = UIColor.init(hex: 0xEF5F67)!
        }else if self.summaryTitle == "说说我自己"{
            introCellColor = UIColor.init(hex: 0x5189EB)!
        }else{
            introCellColor = UIColor.init(hex: 0xBC50F5)!
        }
        
        if self.title.isEmpty{
            
            introCellHeight = 50
            
            return
        }
        
        
        let p = NSMutableParagraphStyle()
        
        p.lineSpacing = 5
        
        titleAttribute = self.title.withAttributes([Attribute.textColor(UIColor.init(hexString: "#333333")!),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.paragraphStyle(p)])
        
        titleHeight = titleAttribute.height(withConstrainedWidth: ScreenWidth - 36 - 29)
        
//
        introCellHeight = 45 + 25 + titleHeight
        
    }
    
    
}
